package cn.xfdzx.android.apps.shop.presenter;

import android.util.Log;

import java.util.Map;

import cn.xfdzx.android.apps.shop.base.BasePresenter;
import cn.xfdzx.android.apps.shop.bean.GoodsLiblaryListBean;
import cn.xfdzx.android.apps.shop.bean.GoodsUpStateBeen;
import cn.xfdzx.android.apps.shop.bean.NewOrderBeen;
import cn.xfdzx.android.apps.shop.bean.OneCategoryBeen;
import cn.xfdzx.android.apps.shop.bean.OrderListBeen;
import cn.xfdzx.android.apps.shop.bean.ShopAddGoodTypeBeen;
import cn.xfdzx.android.apps.shop.bean.ShopInGoodListBeen;
import cn.xfdzx.android.apps.shop.bean.UpdateNumPriceBeen;
import cn.xfdzx.android.apps.shop.contract.OrderListContract;
import cn.xfdzx.android.apps.shop.contract.ShopInGoodListContract;
import cn.xfdzx.android.apps.shop.model.OrderListModel;
import cn.xfdzx.android.apps.shop.model.ShopInGoodListModel;
import cn.xfdzx.android.apps.shop.net.RxScheduler;
import io.reactivex.functions.Consumer;

/**
 * @author azheng
 * @date 2018/6/4.
 * Description：
 */
public class ShopInGoodListPresenter extends BasePresenter<ShopInGoodListContract.View> implements ShopInGoodListContract.Presenter {

    private ShopInGoodListContract.Model model;

    public ShopInGoodListPresenter() {
        model = new ShopInGoodListModel();
    }


    @Override
    public void initData(boolean isRefresh, boolean isLoading, Map<String,Object> map) {
        if (!isViewAttached()) {
            return;
        }

        if (isLoading) {
            mView.showLoading();
        }
        model.initData(map)
                .compose(RxScheduler.<ShopInGoodListBeen>Flo_io_main())
                .as(mView.<ShopInGoodListBeen>bindAutoDispose())
                .subscribe(new Consumer<ShopInGoodListBeen>() {
                    @Override
                    public void accept(ShopInGoodListBeen bean) throws Exception {
                        mView.onGoodListSuccess(bean);
                        if (isLoading) {

                            mView.hideLoading();}

                        if (isRefresh) {
                            mView.finishRefresh();
                        } else {
                            mView.finishLoadMore();
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.onError(throwable);
                        if (isLoading) {

                            mView.hideLoading();}

                        if (isRefresh) {
                            mView.finishRefresh();
                        } else {
                            mView.finishLoadMore();
                        }
                    }
                });
    }

    @Override
    public void goodsUpState(String id, String state, int position) {
        if (!isViewAttached()) {
            return;
        }
        mView.showLoading();
        model.goodsUpState(id, state)
                .compose(RxScheduler.<GoodsUpStateBeen>Flo_io_main())
                .as(mView.<GoodsUpStateBeen>bindAutoDispose())
                .subscribe(new Consumer<GoodsUpStateBeen>() {
                    @Override
                    public void accept(GoodsUpStateBeen bean) throws Exception {
                        mView.onGoodsUpStateSuccess(bean,position);
                        mView.hideLoading();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.onError(throwable);
                        mView.hideLoading();
                    }
                });
    }

    @Override
    public void updateNumPrice(String id, String marketprice, String stock, String discountprice) {
        if (!isViewAttached()) {
            return;
        }
        mView.showLoading();
        model.updateNumPrice(id, marketprice, stock, discountprice)
                .compose(RxScheduler.<UpdateNumPriceBeen>Flo_io_main())
                .as(mView.<UpdateNumPriceBeen>bindAutoDispose())
                .subscribe(new Consumer<UpdateNumPriceBeen>() {
                    @Override
                    public void accept(UpdateNumPriceBeen bean) throws Exception {
                        mView.onUpdateNumPriceSuccess(bean);
                        mView.hideLoading();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.onError(throwable);
                        mView.hideLoading();
                    }
                });
    }

    /**商品库请求成功*/
    @Override
    public void getLibraryList(boolean isRefresh, boolean isLoading,Map<String, Object> map) {
        if (!isViewAttached()) {
            return;
        }
        if (isLoading) {
            mView.showLoading();
        }
        model.getLibraryList(map)
                .compose(RxScheduler.<GoodsLiblaryListBean>Flo_io_main())
                .as(mView.<GoodsLiblaryListBean>bindAutoDispose())
                .subscribe(new Consumer<GoodsLiblaryListBean>() {
                    @Override
                    public void accept(GoodsLiblaryListBean bean) throws Exception {
                        mView.getLibraryListSuccess(bean);
                        if (isLoading) {
                            mView.hideLoading();}
                        if (isRefresh) {
                            mView.finishRefresh();
                        } else {
                            mView.finishLoadMore();
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.onError(throwable);
                        if (isLoading) {
                            mView.hideLoading();}
                        if (isRefresh) {
                            mView.finishRefresh();
                        } else {
                            mView.finishLoadMore();
                        }
                    }
                });
    }

    @Override
    public void getOneCategory(Map<String, Object> map) {
        if (!isViewAttached()) {
            return;
        }
        //mView.showLoading();
        model.getOneCategory(map)
                .compose(RxScheduler.<OneCategoryBeen>Flo_io_main())
                .as(mView.<OneCategoryBeen>bindAutoDispose())
                .subscribe(new Consumer<OneCategoryBeen>() {
                    @Override
                    public void accept(OneCategoryBeen bean) throws Exception {
                        mView.getOneCategorySuccess(bean);
                        mView.hideLoading();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.onError(throwable);
                        mView.hideLoading();
                    }
                });
    }

    @Override
    public void addGoods(Map<String, Object> map,String catoryId) {
        if (!isViewAttached()) {
            return;
        }
        mView.showLoading();
        model.addGoods(map)
                .compose(RxScheduler.<OneCategoryBeen>Flo_io_main())
                .as(mView.<OneCategoryBeen>bindAutoDispose())
                .subscribe(new Consumer<OneCategoryBeen>() {
                    @Override
                    public void accept(OneCategoryBeen bean) throws Exception {
                        mView.addGoodsSuccess(bean,catoryId);
                        mView.hideLoading();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        Log.e("Throwable",throwable.toString()+"=-----");
                        mView.onError(throwable);
                        mView.hideLoading();
                    }
                });
    }

    @Override
    public void getCategoryList() {
        if (!isViewAttached()) {
            return;
        }
        mView.showLoading();
        model.getCategory_list()
                .compose(RxScheduler.<ShopAddGoodTypeBeen>Flo_io_main())
                .as(mView.<ShopAddGoodTypeBeen>bindAutoDispose())
                .subscribe(new Consumer<ShopAddGoodTypeBeen>() {
                    @Override
                    public void accept(ShopAddGoodTypeBeen bean) throws Exception {
                        mView.getCategoryListSuccess(bean);
                        mView.hideLoading();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.onError(throwable);
                        mView.hideLoading();
                    }
                });
    }


}
